﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PuzzleDiamonds
{
    public class Screen
    {
        private int m_nScreenID;
        protected Canvas m_screenCanvas;

        public int ID
        {
            get { return m_nScreenID; }
        }

        public Screen(int pScreenID, Canvas pScreenCanvas)
        {
            m_nScreenID = pScreenID;
            m_screenCanvas = pScreenCanvas;
            m_screenCanvas.Visibility = Visibility.Collapsed;
            
            // add handlers...
            m_screenCanvas.MouseLeftButtonUp += OnMouseLeftButtonUp;
            m_screenCanvas.MouseMove += OnMouseMove;
        }

        protected void OnMouseLeftButtonUp(object o, MouseEventArgs e)
        {
            OnMouseLeftClick(e.GetPosition(m_screenCanvas).X, e.GetPosition(m_screenCanvas).Y);
        }

        protected void OnMouseMove(object o, MouseEventArgs e)
        {
            OnMouseMove(e.GetPosition(m_screenCanvas).X, e.GetPosition(m_screenCanvas).Y);
        }

        public void Activate()
        {
            OnActivate();

            m_screenCanvas.Width = ((Canvas)m_screenCanvas.Parent).Width; // -16; // TODO this sux...
            m_screenCanvas.Height = ((Canvas)m_screenCanvas.Parent).Height; // -16; // TODO this sux...

            m_screenCanvas.Visibility = Visibility.Visible;
        }

        public void Deactivate()
        {
            m_screenCanvas.Visibility = Visibility.Collapsed;
        }

        public virtual void OnActivate()
        {

        }

        public virtual void OnMouseMove(double pMouseX, double pMouseY)
        {

        }

        public virtual void OnMouseLeftClick(double pMouseX, double pMouseY)
        {

        }

        public virtual void Update(double pTick)
        {

        }
    }

    public class ScreenManager
    {
        private static ScreenManager m_instance = null;
        private Screen m_activeScreen;
        private Collection<Screen> m_screenCollection = new Collection<Screen>();

        public Screen ActiveScreen
        {
            get { return m_activeScreen; }
        }

        protected ScreenManager()
        {
            m_activeScreen = null;
        }

        public static ScreenManager Instance()
        {
            if (m_instance == null)
                m_instance = new ScreenManager();
            return m_instance;
        }

        public void AddScreen(Screen pScreen)
        {
            m_screenCollection.Add(pScreen);
        }

        public void ActivateScreen(int pScreenID)
        {
            foreach (Screen screen in m_screenCollection)
            {
                if (screen.ID == pScreenID)
                {
                    if (m_activeScreen != null)
                        m_activeScreen.Deactivate();

                    m_activeScreen = screen;
                    m_activeScreen.Activate();

                    return;
                }
            }
        }

        public void Update(double pTick)
        {
            m_activeScreen.Update(pTick);
        }
    }
}
